package com.ea.eadp.nexus.test.common;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

import com.ea.eadp.nexus.client.nucleus.identity.model.MachineProfileCheckVerifiedApiResponse;
import com.ea.eadp.nexus.test.common.ApiClients.NucleusApi;

public class MachineProfileTestHelper {
    public static String getDeviceIdFromMachineProfileCookie(WebDriver driver) {
        return getDeviceIdFromMachineProfileCookie(driver, true);
    }
    public static String getDeviceIdFromMachineProfileCookie(WebDriver driver, boolean needNavigation) {
        Cookie deviceIdCookie = getMachineProfileCookie(driver, needNavigation);
        String deviceId = null;
        if(deviceIdCookie != null) {
            deviceId = deviceIdCookie.getValue();
        }
        return deviceId;
    }
    
    public static Cookie getMachineProfileCookie(WebDriver driver, boolean needNavigation){
        if(needNavigation) driver.navigate().to(TestUtil.BasePortalURI() + "/p/");
        Cookie deviceIdCookie = null;
        deviceIdCookie = driver.manage().getCookieNamed(Constant.EA_LOGIN_WEB_KEY);
        return deviceIdCookie;
    }
    
    
    public static void setMachineProfileCookie(WebDriver driver, Cookie deviceIdCookie, boolean needNavigation) {
        if(needNavigation) driver.navigate().to(TestUtil.BasePortalURI() + "/p/");      
        Assert.assertNotNull(deviceIdCookie);
        Cookie newDeviceCookie = new Cookie(
                deviceIdCookie.getName(), 
                deviceIdCookie.getValue(), 
                deviceIdCookie.getDomain(), 
                deviceIdCookie.getPath(), 
                deviceIdCookie.getExpiry(), 
                deviceIdCookie.isSecure());
        driver.manage().addCookie(newDeviceCookie);       
    }

    /**
     * Validate machine profile status
     * @param pid
     * @param deviceId
     * @param platformType
     * @param profileType
     * @param isVerified
     * @throws Exception
     */
    public static void validateMachineProfileStatus(long pid, String deviceId, String platformType, String profileType, boolean isVerified) throws Exception {
        Assert.assertNotNull(deviceId, "DeviceId should not be null");
        Assert.assertNotNull(platformType, "platformType should not be null");
        Assert.assertNotNull(profileType, "profileType should not be null");
        MachineProfileCheckVerifiedApiResponse response = NucleusApi.instance().getMachineProfile(
                String.valueOf(pid),
                deviceId,
                platformType,
                profileType);
        Assert.assertEquals(response.getHttpResponseCode(), 200);
        String expectedStatus = isVerified ? "VERIFIED" : "UNVERIFIED";
        Assert.assertEquals(response.getMachineProfileResponse().getStatus(), expectedStatus);
    }
    
    /**
     * Check device status does not exist
     * @param pid
     * @param deviceId
     * @param platformType
     * @param profileType
     * @throws Exception
     */
    public static void verifyMachineProfileNotExist(long pid, String deviceId, String platformType, String profileType) throws Exception {
        MachineProfileCheckVerifiedApiResponse response = NucleusApi.instance().getMachineProfile(
                String.valueOf(pid),
                deviceId, 
                platformType,
                profileType);
        Assert.assertEquals(response.getHttpResponseCode(), 404);
    }
}
